package com.wangjiacai.utils.mail.impl;

import com.wangjiacai.utils.mail.MailSenderService;
import com.wangjiacai.utils.utils.FileUtil;
import com.wangjiacai.utils.web.exception.CodeMessageException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class MailSenderServiceImpl implements MailSenderService {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Override
    public void sendMail(String from, String to, String subject, String msg) {
        MimeMessage mimeMessage = getMimeMessage(from);
        sendMail(mimeMessage,to,subject,msg);
    }

    @Override
    public void sendMail(String from, String username,String to, String subject, String msg){
        MimeMessage mimeMessage = getMimeMessage(from,username);
        sendMail(mimeMessage,to,subject,msg);
    }

    private void sendMail(MimeMessage mimeMessage,String to, String subject, String msg){
        try {
            mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            mimeMessage.setSubject(subject,"UTF-8");
            mimeMessage.setText(msg,"UTF-8");
        } catch (MessagingException e) {
            e.printStackTrace();
            throw new CodeMessageException(e.getMessage());
        }
        mailSender.send(mimeMessage);
    }

    @Override
    public void sendMail(String from, String to, String subject, String msg, File[] files){
        MimeMessage mimeMessage = getMimeMessage(from);
        sendMail(from,mimeMessage,to,subject,msg,files);
    }

    @Override
    public void sendMail(String from,String username,String to, String subject, String msg, File[] files){
        MimeMessage mimeMessage = getMimeMessage(from,username);
        sendMail(from,mimeMessage,to,subject,msg,files);
    }

    private void sendMail(String from,MimeMessage mimeMessage,String to, String subject, String msg, File[] files) {
        MimeMessageHelper mimeMessageHelper = null;
        try {
            mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(msg);
            for (File file : files) {
                if (!FileUtil.getFileExt(file.getName()).equals("zip")) {
                    mimeMessageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), new FileSystemResource(file));
                }
            }
        } catch (MessagingException e) {
            e.printStackTrace();
            throw new CodeMessageException(e.getMessage());
        }catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new CodeMessageException(e.getMessage());
        }
        mailSender.send(mimeMessage);
    }

    @Override
    public void sendMail(String from, String to, String subject, String msg, Object model) {
        MimeMessage mimeMessage = getMimeMessage(from);
        sendMail(from,mimeMessage,to,subject,msg,model);
    }

    @Override
    public void sendMail(String from, String username,String to, String subject, String msg, Object model){
        MimeMessage mimeMessage = getMimeMessage(from,username);
        sendMail(from,mimeMessage,to,subject,msg,model);
    }

    private void sendMail(String from, MimeMessage mimeMessage,String to, String subject, String msg, Object model){
        MimeMessageHelper mimeMessageHelper = null;
        try {
            mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(getTemplateText(msg, model), true);
        } catch (MessagingException e) {
            e.printStackTrace();
            throw new CodeMessageException(e.getMessage());
        }

        mailSender.send(mimeMessage);
    }

    @Override
    public void sendMail(String from, String to, String subject, String msg, File[] files, Object model){
        MimeMessage mimeMessage = getMimeMessage(from);
        sendMail(from,mimeMessage,to,subject,msg,files,model);
    }

    @Override
    public void sendMail(String from, String username,String to, String subject, String msg, File[] files, Object model) {
        MimeMessage mimeMessage = getMimeMessage(from,username);
        sendMail(from,mimeMessage,to,subject,msg,files,model);
    }

    private void sendMail(String from,MimeMessage mimeMessage,String to, String subject, String msg, File[] files, Object model){
        MimeMessageHelper mimeMessageHelper = null;
        try {
            mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(getTemplateText(msg, model), true);
            System.getProperties().setProperty("mail.mime.splitlongparameters", "false");
            for (File file : files) {
                if (!FileUtil.getFileExt(file.getName()).equals("zip")) {
                    mimeMessageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), new FileSystemResource(file));
                }
            }
            mailSender.send(mimeMessage);
        } catch (MessagingException |UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new CodeMessageException(e.getMessage());
        }


    }

    private String getTemplateText(String path, Object model) {
        Template template = null;
        try {
            template = freeMarkerConfigurer.getConfiguration().getTemplate(path);
            return FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
        } catch (IOException |TemplateException e) {
            e.printStackTrace();
            throw new CodeMessageException(e.getMessage());
        }
    }

    private MimeMessage getMimeMessage(String from, String username) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            mimeMessage.setFrom(new InternetAddress(from,username,"UTF-8"));
        } catch (MessagingException e) {
            e.printStackTrace();
            throw new CodeMessageException(e.getMessage());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new CodeMessageException(e.getMessage());
        }
        return mimeMessage;
    }

    private MimeMessage getMimeMessage(String from) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            mimeMessage.setFrom(new InternetAddress(from));
        } catch (MessagingException e) {
            e.printStackTrace();
            throw new CodeMessageException(e.getMessage());
        }
        return mimeMessage;
    }
}
